﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using e_commerce.Models;
using System.Web.Mvc;
using e_commerce.BLL.Services.Enterprise;
using e_commerce.BLL;
using Newtonsoft.Json;
using e_commerce.BLL.Data;
using e_commerce.Filter;
using e_commerce.BLL.Models;

namespace e_commerce.Controllers
{
    public class ManageController : Controller
    {
        private EmployeeService _eService;
        private PermissionsService _pService;
        
        public ManageController()
        {
            TempData["f-nav"] = "人员管理";
            _eService = new EmployeeService();
            _pService = new PermissionsService();
        }

        private void SetTempData(string fnav, string snav)
        {
            TempData["f-nav"] = fnav;
            TempData["s-nav"] = snav;
        }
        // GET: Manage
        [PermissionFilter(permission: Permission.EmployeeManage_All)]
        public ActionResult All(int page =1)
        {
            SetTempData("人员管理", "全部员工");
            int total = 1;
            int pageSize = 8;
            List<Models.Employee> list = _eService.GetEmployeeByPage(page, pageSize,ref total).Select(e=>new Models.Employee()
            {
                Id = e.Id,
                Name = e.User.NickName,
                Gender = e.User.Gender == false?"男":"女",
                PhoneNumber = e.User.PhoneNumber,
                Email = e.User.Email,
            }).ToList();
            var staticList = new PagedList.StaticPagedList<Models.Employee>(list, page, pageSize, total);
            return View(staticList);
        }

        [PermissionFilter(permission: Permission.EmployeeManage_Add)]
        public ActionResult Add()
        {
            SetTempData("人员管理", "新增员工");
            return View();
        }

        [HttpGet]
        [PermissionFilter(permission: Permission.EmployeeManage_All)]
        public JsonResult GetAllPermissions()
        {
            var pTree = _pService.GetAllPermissons();
            JsonResult res = new JsonResult();
            res.Data = pTree;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }


        /// <summary>
        /// 用户信息
        /// </summary>
        /// <returns></returns>
        [PermissionFilter(permission: Permission.EmployeeManage_Personal)]
        public ActionResult Personal()
        {
            var identityUser = Session["ADMIN"] as AdminUser;
            
            return View();
        }

        /// <summary>
        /// 用户信息改动
        /// </summary>
        /// <param name="user">改动信息传入</param>
        /// <returns></returns>
        public ActionResult ModifyUserInformation(AdminUser user)
        {
            return null;
        }

        /// <summary>
        /// 修改用户权限
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="permissions">用户权限</param>
        /// <returns></returns>
        public JsonResult ModifyUserPermissions(int userId,int[] permissions)
        {
            return null;
        }

        public JsonResult ModifyUserRoles(int userId,int[] roles)
        {
            return null;
        }

        /// <summary>
        /// 通过员工id返回员工信息
        /// </summary>
        /// <param name="empolyeeId">员工id</param>
        /// <returns>返回一个json数据</returns>
        [PermissionFilter(permission: Permission.EmployeeManage_All)]
        public ActionResult GetEmpolyeePermissions(int employeeId)
        {
            var permissions = _pService.GetEmployeePermissions(employeeId);
            JsonResult res = new JsonResult();
            res.Data = permissions;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }

        [HttpPost]
        [PermissionFilter(permission: Permission.EmployeeManage_All)]
        public ActionResult GetEmployeeRoles(int employeeId)
        {
            var roles = _pService.GetEmployeeRoles(employeeId).Select(r=>new RoleModel()
            {
                Id = r.RoleId,
                Name = r.RoleName,
            });
            return PartialView("~/Views/Manage/ViewComponents/RoleList.cshtml",roles);

        }

        /// <summary>
        /// 增加员工
        /// </summary>
        /// <param name="employee">员工模型</param>
        /// <returns></returns>
        [HttpPost]
        [PermissionFilter(permission: Permission.EmployeeManage_Add)]
        public JsonResult AddEmpolyee(Models.Employee employee)
        {
            var res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                User user = new User()
                {
                    Account = employee.Name,
                    NickName = employee.Name,
                    Gender = employee.Gender == "男" ? true : false,
                    PhoneNumber = employee.PhoneNumber,
                    Email = employee.Email,
                };
                var id = _eService.CreateEmployee(user);
                 _pService.ConfigPermission(id, employee.Permissions);
                success = "创建成功";
            }catch(Exception e)
            {
                error = e.Message;
            }
            res.Data = new { success, error };
            return res;
        }


        [HttpPost]
        public JsonResult DeleteEmployee(int employeeId)
        {
            var res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                _eService.DeleteEmployee(employeeId);
                success = "删除成功";
            }
            catch (Exception e)
            {
                error = e.Message;
            }
            res.Data = new { success, error };
            return res;
        }
    }
}